
function test2(){

    // var Run = Java.type("java.lang.Runnable");
    // var MyRun = Java.extend(Run)
    // var run1 = new MyRun({
    //     run: function() {
    //         print("Run in separate thread");
    //     }
    // })
    // // run1.run()
    // var Thread = Java.type("java.lang.Thread");
    // var th = new Thread(run1);
    // th.start()
    // Thread.sleep(1000)


    var Runnable = Java.type("java.lang.Runnable");
    var RunnableImpl = Java.extend(Runnable, {
        run: function() {
            print("Run in separate thread");
        }
    })
    var Thread = Java.type("java.lang.Thread");
    var th = new Thread(new RunnableImpl);
    th.start()
    // Thread.sleep(1000)
}

/**
 * 是否close标识,如果close两次会出问题
 */
var startupTimestamp = 0;

function  getStartupTimestamp() {
    return startupTimestamp;
}

 

/**
 * Lua构造函数
 * @param scriptName
 * @param taskKey
 * @param isIE11
 */

function isLuaProcessClosed(){
    return !lp.IsExist();
}

function sendAndReceive(map) {
    if(isLuaProcessClosed()) {
        //BACR_99601_80:lua因执行超过15分钟，被守护线程干掉
        throwBankExceptionByErrorCode(BACR_99601_80);
    }
    lp.Send(JSON.toJSONString(map));
    if (map.containsKey("password")) {
        map.put("password", "*******");
    }
    logger.info("send msg : {}", JSON.toJSONString(map));
    return lp.Receive();
}

/**
 * 直接使用ie打开URL
 *
 * @param url
 * @return
 */
function  getshow(url) {
    var map = new LinkedHashMap();
    map.put("method", "getshow");
    map.put("url", url);
    return sendAndReceive(map);
}

/**
 * 直接使用ie打开URL，不显示窗口
 */
function  get( url) {
    var map = new LinkedHashMap();
    map.put("method", "get");
    map.put("url", url);
    return sendAndReceive(map);
}

/**
 * 显示窗口,一直获得焦点
 */
function  show() {
    var map = new LinkedHashMap();
    map.put("method", "show");
    return sendAndReceive(map);
}

/**
 * 显示窗口,只获得一次焦点
 */
function  show_noff() {
    var map = new LinkedHashMap();
    map.put("method", "show_noff");
    return sendAndReceive(map);
}

/**
 * 隐藏窗口
 */
function  hide() {
    var map = new LinkedHashMap();
    map.put("method", "hide");
    return sendAndReceive(map);
}

/**
 * 统一输出日志，方便看流程
 *
 * @param map
 * @return
 */
function  parserSend( map) {
    var msg = JSON.toJSONString(map);
    if (map.containsKey("password")) {
        var password = map.get("password");
        if (password !== null) {
            password = password.replaceAll("\\w", "*");
        }
        map.put("password", password);
    }
    if (map.containsKey("text")) {
        var text = map.get("text");
        if (text !== null) {
            text = text.replaceAll("\\w", "*");
        }
        map.put("text", text);
    }
    logger.info("send msg : {}", JSON.toJSONString(map));
    return msg;
}

/**
 * 向指定ID的文本框输入值
 *
 * @param id
 * @param text
 * @return
 */
function  set_input_text_by_id( id,  text) {
   var map = new LinkedHashMap();
    map.put("method", "set_input_text_by_id");
    map.put("id", id);
    map.put("text", text);
    return sendAndReceive(map);
}

/**
 * 设置代理
 *
 * @param address
 * @param port
 * @return
 */
function  set_proxy( address,  port) {
    logger.info("使用代理 IP : {} , port : {}", address, port);
    var map = new LinkedHashMap();
    map.put("method", "set_proxy");
    map.put("address", address);
    map.put("port", port);
    return sendAndReceive(map);
}

/**
 * 设置代理
 *
 * @param address
 * @param port
 * @param username
 *            代理用户名
 * @param password
 *            代理密码
 * @return
 */
function  set_proxy_password( address,  port,  username,  password) {
    logger.info("使用代理 IP : {} , port : {} with password", address, port);
    var map = new LinkedHashMap();
    map.put("method", "set_proxy_password");
    map.put("address", address);
    map.put("port", port);
    map.put("username", username);
    map.put("password", password);
    return sendAndReceive(map);
}

/**
 * 获取弹出对话框信息，会等待100ms
 *
 * @return
 */
function  get_alert_msg() {
    var map = new LinkedHashMap();
    map.put("method", "get_alert_msg");
    var msg = sendAndReceive(map);
    if (equals(msg,"no")) {
        msg = "";
    }
    return msg;
}

/**
 * 获取浏览器当前URL
 *
 * @return
 */
function  get_url() {
    var map= new LinkedHashMap();
    map.put("method", "get_url");
    return sendAndReceive(map);
}

/**
 * 判断ID是否存在
 *
 * @param id
 * @return
 */
function  is_exist_by_id( id) {
    var map = new LinkedHashMap();
    map.put("method", "is_exist_by_id");
    map.put("id", id);
    var result = sendAndReceive(map);
    if (StringUtils.isNotBlank(result) && equals(result,"ok")) {
        return true;
    }
    return false;
}

/**
 * frame中是否存在某个ID
 *
 * @param id
 * @param frames
 * @return
 */
function  frame_is_exist_by_id( id,  frames) {
    var map = new LinkedHashMap();
    map.put("method", "frame_is_exist_by_id");
    map.put("id", id);
    map.put("frame", frames);
    var result = sendAndReceive(map);
    if (StringUtils.isNotBlank(result) && equals(result,"ok")) {
        return true;
    }
    return false;
}

/**
 * 向指定ID的文本框输入值后调用改变事件
 *
 * @param id
 * @param text
 * @return
 */
function  set_input_text_by_id_event( id,  text) {
    var map = new LinkedHashMap();
    map.put("method", "set_input_text_by_id_event");
    map.put("id", id);
    map.put("text", text);
    return sendAndReceive(map);
}

/**
 * 获取指定ID的HTML
 *
 * @param id
 * @param text
 * @return
 */
function  get_html_by_id( id) {
    var map = new LinkedHashMap();
    map.put("method", "get_html_by_id");
    map.put("id", id);
    var result = sendAndReceive(map);
    if (StringUtils.isBlank(result) || equals(result,"get id fail") || equals(result,"get element error")) {
        return "";
    }
    return result;
}

/**
 * 根据name修改标签的id,默认选择第一个
 *
 * @param name
 * @return
 */
function  change_id_by_name( name) {
    var map = new LinkedHashMap();
    map.put("method", "change_id_by_name");
    map.put("name", name);
    return sendAndReceive(map);
}

/**
 * 向指定ID的文本框输入值
 *
 * @param id
 * @param text
 * @return
 */
function  set_frame_input_text_by_id( id,  text,  frames) {
    var map = new LinkedHashMap();
    map.put("method", "set_frame_input_text_by_id");
    map.put("id", id);
    map.put("text", text);
    map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 向指定ID的文本框输入值后调用改变事件
 *
 * @param id
 * @param text
 * @return
 */
function  set_frame_input_text_by_id_event( id,  text,  frames) {
    var map = new LinkedHashMap();
    map.put("method", "set_frame_input_text_by_id_event");
    map.put("id", id);
    map.put("text", text);
    map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 通过ajax方式get,注意跨域<br>
 * 在调用方法之前会进行域名校验,如果域名不相等,会返回跨域错误
 *
 * @param url
 * @return
 */
function  ajax_get(url) {
    var map = new LinkedHashMap();
    map.put("method", "ajax_get");
    map.put("url", url);
    return sendAndReceive(map);
}

/**
 * 通过lua的ajax方式get,注意跨域<br>
 * 在调用方法之前会进行域名校验,如果域名不相等,会返回跨域错误
 *
 * @param url
 * @return
 */
function  lua_ajax_get(url) {
    var map = new LinkedHashMap();
    map.put("method", "lua_ajax_get");
    map.put("url", url);
    return sendAndReceive(map);
}

/**
 * 通过ajax方式post json,注意跨域<br>
 * 在调用方法之前会进行域名校验,如果域名不相等,会返回跨域错误
 *
 * @param url
 * @param json
 * @return
 */
function  ajax_post_json(url,json) {
    var map = new LinkedHashMap();
    map.put("method", "ajax_post_json");
    map.put("url", url);
    map.put("json", json);
    return sendAndReceive(map);
}

/**
 * 通过ajax方式post,注意跨域<br>
 * 在调用方法之前会进行域名校验,如果域名不相等,会返回跨域错误
 *
 * @param url
 * @param postdata
 * @return
 */
function  ajax_post( url, postdata) {
    var map = new LinkedHashMap();
    var sb = new StringBuffer();
    if(postdata!=undefined||postdata!=null){
        for each(var key in postdata.keySet()) {
            sb.append(key + "=" + postdata.get(key) + "&");
        }
    }
    var data = "";
    if (sb.length() > 0) {
        data = sb.substring(0, sb.length() - 1);
    }
    map.put("method", "ajax_post");
    map.put("url", url);
    map.put("data", data);
    return sendAndReceive(map);
}

function  ajax_post_file( url,  postdata) {
    var map = new LinkedHashMap();
    var sb = new StringBuffer();
    for each(var key in postdata.keySet()) {
        sb.append(key + "=" + postdata.get(key) + "&");
    }
    var data = "";
    if (sb.length() > 0) {
        data = sb.substring(0, sb.length() - 1);
    }
    map.put("method", "ajax_post_file");
    map.put("url", url);
    map.put("data", data);
    return sendAndReceive(map);
}

/**
 * 在frame上注入js，然后ajax方式get，为了规避跨域
 *
 * @param url
 * @param frames
 * @return
 */
function  ajax_frame_get( url,  frames) {
    var map = new LinkedHashMap();
    map.put("method", "ajax_frame_get");
    map.put("url", url);
    map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 在frame上注入js，然后ajax方式post json，为了规避跨域
 *
 * @param url
 * @param json
 * @param frames
 * @return
 */
function  ajax_frame_post_json( url,  json,  frames) {
    var map = new LinkedHashMap();
    map.put("method", "ajax_frame_post_json");
    map.put("url", url);
    map.put("json", json);
    map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 在frame上注入js，然后ajax方式post，为了规避跨域
 *
 * @param url
 * @param postdata
 * @param frames
 * @return
 */
function  ajax_frame_post( url, postdata,  frames) {
    var map = new LinkedHashMap();
    var sb = new StringBuffer();
    for each(var key in postdata.keySet()) {
        sb.append(key + "=" + postdata.get(key) + "&");
    }
    var data = "";
    if (sb.length() > 0) {
        data = sb.substring(0, sb.length() - 1);
    }
    map.put("method", "ajax_frame_post");
    map.put("url", url);
    map.put("data", data);
    map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 通过id点击按钮
 *
 * @param id
 * @return
 */
function  click_by_id( id) {
    var map = new LinkedHashMap();
    map.put("method", "click_by_id");
    map.put("click_id", id);
    return sendAndReceive(map);
}

/**
 * 截图,然后将当前页面保存到文件
 *
 * @param id
 * @return
 */
function  screen_print() {
    var map = new LinkedHashMap();
    map.put("method", "screen_print");
    return sendAndReceive(map);
}

/**
 * 截全图,然后将当前页面保存到文件
 *
 * @param id
 * @return
 */
function  screen_full_print() {
    var map = new LinkedHashMap();
    map.put("method", "screen_full_print");
    return sendAndReceive(map);
}

/**
 * 通过标签文字点击
 *
 * @param id
 * @return
 */
function  click_by_tag_text(tag,text) {
    var map = new LinkedHashMap();
    map.put("method", "click_by_tag_text");
    map.put("tag", tag);
    map.put("text", text);
    return sendAndReceive(map);
}

/**
 * 通过标签名称获取所有的id
 *
 * @param tag
 * @return
 */
function  get_ids_by_tag(tag) {
    var map = new LinkedHashMap();
        map.put("method", "get_ids_by_tag");
        map.put("tag", tag);
    return sendAndReceive(map);
}

/**
 * 通过id点击按钮
 *
 * @param id
 * @return
 */
function  click_frame_by_id(id,frames) {
    var map = new LinkedHashMap();
        map.put("method", "click_frame_by_id");
        map.put("click_id", id);
        map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 通过ID获取图片路径
 *
 * @param id
 * @return
 */
function  get_image_by_id(id) {

    var map = new LinkedHashMap();
        map. put("method", "get_image_by_id");
        map.put("image_id", id);
    return sendAndReceive(map);
}

/**
 * hide下通过id截图
 *
 * @param id
 * @return
 */
function  get_hide_image_by_id(id) {

    var map = new LinkedHashMap();
        map.put("method", "get_hide_image_by_id");
        map.put("image_id", id);
    return sendAndReceive(map);
}

/**
 * 通过class方式点击，默认选择第一个class
 *
 * @param className
 * @return
 */
function  click_by_class(className) {

    var map = new LinkedHashMap();
    map.put("method", "click_by_class");;
    map.put("class_name", className);
    return sendAndReceive(map);
}

/**
 * 通过点击ID进行下载
 *
 * @param clickId
 * @return
 */
function  download_by_click_id(clickId) {
    var map = new LinkedHashMap();
    map.put("method", "download_by_click_id");
    map.put("click_id", clickId);
    return sendAndReceive(map);
}

/**
 * 通过点击ID进行下载
 *
 * @param clickId
 * @return
 */
function  download_by_click_element(tableid,num) {

    var map = new LinkedHashMap();
        map.put("method", "download_by_click_element");
        map.put("table_id", tableid);
        map.put("num", num);
    return sendAndReceive(map);
}

/**
 * 点击frame中的id，然后下载
 *
 * @param clickId
 * @param frames
 * @return
 */
function  frame_download_by_click_id(clickId, frames) {

    var map = new LinkedHashMap();
    map.put("method", "frame_download_by_click_id");
    map.put("click_id", clickId);
    map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 单击后等待出现某个ID
 *
 * @param click_id
 * @param wait_id
 * @return
 */
function  click_by_id_wait_id(click_id,wait_id) {
    var map = new LinkedHashMap();
        map.put("method", "click_by_id_wait_id");
        map.put("click_id", click_id);
        map.put("wait_id", wait_id);
    return sendAndReceive(map);
}

/**
 * 关闭脚本<br>
 * 如果正常结束,那么需要发送close方法,结束监听<br>
 * 如果被守护线程结束,不发送,因为发送可能导致守护线程卡死<br>
 *
 * @return
 */
function  close() {
    var map = new LinkedHashMap();
    map.put("method", "close");
    map.put("isEnd", "1");
    var isDaemon = Thread.currentThread().isDaemon();
    var msg = "ok";
    // 如果调用的是守护线程,那么不发送,只打印日志
    if (isDaemon) {
        map.put("warn", "当前线程被守护线程调用,这条消息是为了查看退出方法是否执行,其实不执行");
        parserSend(map);
    } else {// 不是守护线程调用,发送,正常结束
        msg = sendAndReceive(map);
    }
    // exit();
    return msg;
}


/**
 * 输入密码
 *
 * @param password
 * @return
 */
function  input_password(password) {

    var map = new LinkedHashMap();
        map.put("method", "input_password");
        map.put("password", password);
    return sendAndReceive(map);
}

/**
 * 按删除键多少次
 *
 * @param count
 * @return
 */
function  press_back_space(count) {

    var map = new LinkedHashMap();
    map.put("method", "press_back_space");
    map.put("count", String.valueOf(count));
    return sendAndReceive(map);
}

/**
 * 获取当前页面内容
 *
 * @return
 */
function  get_html() {

    var map = new LinkedHashMap();
    map.put("method", "get_html");
    return sendAndReceive(map);

}

/**
 * 获取当前页面中某个frame内容
 *
 * @return
 */
function  get_frame_html(frames) {
   var map = new LinkedHashMap();
    map.put("method", "get_frame_html");
    map.put("frame", frames);
    return sendAndReceive(map);
}

/**
 * 获取cookie，http-only属性不能获得
 *
 * @return
 */
function  get_cookie() {
   var map =  new LinkedHashMap();
   map.put("method", "get_cookie");
    var cookie = sendAndReceive(map);
    var cookies = cookie.split("; ");
    var cookieMap = new LinkedHashMap();
    for each(var string in cookies) {
        if (StringUtils.isNotBlank(string)) {
            var coo = string.split("=");
            if (coo.length == 2) {
                cookieMap.put(coo[0].trim(), coo[1].trim());
            } else {
                cookieMap.put(coo[0].trim(), "");
            }
        }
    }
    var sb = new StringBuffer();
    for each(var key in cookieMap.keySet()) {
        sb.append(key + "=" + cookieMap.get(key) + "; ");
    }
    if (sb.length() > 0) {
        cookie = sb.substring(0, sb.length() - 2);
    }
    return cookie;
}

/**
 * 刷新当前页面
 *
 * @return
 */
function refresh() {
    var map = new LinkedHashMap();
    map.put("method", "refresh");
    return sendAndReceive(map);
}

/**
 * 打开url的新窗口
 *
 * @return 1
 */
function  openNewWindow(url) {
    var map = new LinkedHashMap();
        map.put("method", "open_in_new_window");
        map.put("url", url);
    return sendAndReceive(map);
}
